package com.ly.mpayment.rest.system;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.core.Context;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.ly.mpayment.core.util.Config;
import com.ly.mpayment.core.util.JsonUtils;
import com.ly.mpayment.core.util.MapUtil;
import com.ly.mpayment.core.util.ResultMessage;
import com.ly.mpayment.persist.entity.ErrorCode;
import com.ly.mpayment.persist.entity.User;
import com.ly.mpayment.persist.service.errorcode.IErrorCodeService;
import com.ly.mpayment.persist.service.user.IUserService;
import com.ly.mpayment.rest.AbsRest;

import cn.jpush.api.utils.StringUtils;
import net.sf.json.JSONObject;

@Component
@Path("system/errorcode/")
public class ErrorCodeRest extends AbsRest{

	@Autowired
	IUserService userService;

	@Autowired
	IErrorCodeService codeService;

	@POST
	@Path("addErrorCode")
	public String addErrorCode(String data,@Context HttpServletRequest request){
		String result = this.validationParameter(data, request, userService, "errorname","remark","resultcode","useruuid");
		if(result == null){
			JSONObject json = JsonUtils.converToJson(data);
			User user = this.userService.findByUserid(json.getString("useruuid"));
			if(user != null){
				ErrorCode code = this.codeService.findErrorCode(json.getString("resultcode"));
				if(code == null){
					code = new ErrorCode();
					code.setResultcode(json.getString("resultcode"));
					code.setErrorname(json.getString("errorname"));
					code.setRemark(json.getString("remark"));
					code.setUserByUseruuid(user);
					code.setStatus(Config.publicStatus[1]+"");
					codeService.addErrorCode(code);
					return ResultMessage.getSucceed(null);
				}
				return ResultMessage.getFailt(ResultMessage.addErrorCodeExistError);
			}
			return ResultMessage.getFailt(ResultMessage.addErrorCodeShopError);
		}
		return result;
	}

	@POST
	@Path("findPageError")
	public String findPageError(String data,@Context HttpServletRequest request){
		String result = this.validationParameter(data, request, userService, "useruuid","start","pageSize","resultcode");
		if(result == null){
			JSONObject json = JsonUtils.converToJson(data);
			if(!StringUtils.isEmpty(json.getString("useruuid"))){
				Map<String, Object> map = MapUtil.getMap();
				map.put(":useruuid", json.getString("useruuid"));
				if(!StringUtils.isEmpty(json.getString("resultcode"))){
					map.put(":resultcode", json.getString("resultcode"));
				}
				return ResultMessage.getSucceed( JsonUtils.excludes(this.codeService.findPageErrorCode(map,
						json.getInt("start"), json.getInt("pageSize")), new String[]{"createtime","userByUseruuid","status"}));
			}
			return ResultMessage.getFailt(ResultMessage.addErrorCodeShopError);
		}
		return result;
	}

	@POST
	@Path("updateErrorCode")
	public String updateErrorCode(String data,@Context HttpServletRequest request){
		String result = this.validationParameter(data, request, userService, "errorcode","errorname","remark","resultcode","useruuid");
		if(result == null){
			JSONObject json = JsonUtils.converToJson(data);
			User user = this.userService.findByUserid(json.getString("useruuid"));
			if(user != null){
				ErrorCode code = this.codeService.findById(json.getString("errorcode"));
				if(code != null){
					//检查resultcode不能重复
					code.setResultcode(json.getString("resultcode"));
					code.setErrorname(json.getString("errorname"));
					code.setRemark(json.getString("remark"));
					this.codeService.addErrorCode(code);
					return ResultMessage.getSucceed(null);
				}
				return ResultMessage.getFailt("");
			}
			return ResultMessage.getFailt(ResultMessage.addErrorCodeShopError);
		}
		return result;
	}
}
